<template>
	<view class="uni-container">
		<uni-forms ref="form" :value="formData" validateTrigger="bind">
			<uni-forms-item name="question_title" label="问题内容" required>
				<uni-easyinput v-model="formData.question_title" />
			</uni-forms-item>
			<uni-forms-item name="question_type" label="问题类型" required>
				<uni-data-checkbox v-model="formData.question_type" :localdata="formOptions.question_type_localdata" />
			</uni-forms-item>
			<uni-forms-item name="question_required" label="是否必填">
				<switch @change="binddata('question_required', $event.detail.value)" :checked="formData.question_required" />
			</uni-forms-item>

			<view class="uni-button-group">
				<button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
				<navigator open-type="navigateBack" style="margin-left: 15px;">
					<button class="uni-button" style="width: 100px;">返回</button>
				</navigator>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '@/js_sdk/validator/opendb-vote-question.js';

	const db = uniCloud.database();
	const dbCmd = db.command;
	const dbCollectionName = 'opendb-vote-question';
	
	var questionnaire_id = ''

	function getValidator(fields) {
		let reuslt = {}
		for (let key in validator) {
			if (fields.includes(key)) {
				reuslt[key] = validator[key]
			}
		}
		return reuslt
	}

	export default {
		data() {
			return {
				formData: {
					"question_title": "",
					"question_type": "",
					"question_required": null
				},
				formOptions: {
					"question_type_localdata": [{
							"text": "单选",
							"value": "radio"
						},
						{
							"text": "多选",
							"value": "checkbox"
						},
						{
							"text": "填空",
							"value": "input"
						},
						{
							"text": "单选和其他",
							"value": "radioinput"
						},
						{
							"text": "多选和其他",
							"value": "checkboxinput"
						}
					]
				},
				rules: {
					...getValidator(["question_title", "question_type", "question_required"])
				}
			}
		},
		onLoad(e) {
			if (e.questionnaire_id) {
				questionnaire_id = e.questionnaire_id
			}
			else {
				uni.showModal({
					content:"页面参数错误",
					showCancel:false
				})
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			/**
			 * 触发表单提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.submit().then((res) => {
					this.submitForm(res)
				}).catch((errors) => {
					uni.hideLoading()
				})
			},

			submitForm(value) {
				// 使用 uni-clientDB 提交数据
				value.parent_id = questionnaire_id
				value.type = 1
				db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				}).finally(() => {
					uni.hideLoading()
				})
			}
		}
	}
</script>
